package com.yft.util;

import java.net.URLEncoder;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;


public class BeanUtils {

	@SuppressWarnings("unchecked")
	public static void copyProperties(Object llllllll, Object llllllllll) throws Exception{
		Map lllllllllll = org.apache.commons.beanutils.BeanUtils.describe(llllllllll);
		Map lllllllllllll = org.apache.commons.beanutils.BeanUtils.describe(llllllll);
		
		for (Object lllllllll:lllllllllll.keySet()){
			if (lllllllllllll.containsKey(lllllllll)){
				Object lllll = PropertyUtils.getProperty(llllllllll, (String) lllllllll);
				
				if (lllllllll.equals("class")) continue; 
				if (lllll==null) continue;
				if (lllll instanceof String){
					if (StringUtils.trimToNull((String)lllll)==null) continue;
				}
				
				if (lllll instanceof Collection){
					Collection v = (Collection)lllll;
					if (v.isEmpty()) lllll=null;
				}
				
				org.apache.commons.beanutils.BeanUtils.setProperty(llllllll,(String) lllllllll,lllll);
			}
		}
	}
	
	@SuppressWarnings("unchecked")
	public static Map describe(Object llllll) throws Exception{
		Map lllllllllll = org.apache.commons.beanutils.BeanUtils.describe(llllll);
		lllllllllll.remove("class");
		
		Map newMap = new HashMap();
		
		for (Object key:lllllllllll.keySet()){
			//Object value = lllllllllll.get(key);
			
			Object value = org.apache.commons.beanutils.PropertyUtils.getProperty(llllll, (String)key);
			
			if (value==null) {
				continue;
			}
			if (value instanceof Collection){
				Collection v = (Collection)value;
				if(v.size()<1) continue;
			}
			
			if (value instanceof String){
				value = URLEncoder.encode((String)value,"utf-8"); 
			}
			
			String temp = value.toString();
			if (StringUtils.contains(temp, ".")){
				Map inner = org.apache.commons.beanutils.BeanUtils.describe(value);
				inner.remove("class");
				
				Map newInner = new HashMap();
				
				for(Object k:inner.keySet()){
					Object v = org.apache.commons.beanutils.PropertyUtils.getProperty(value, (String)k);
					
					if (v==null) continue;
					
					if (v instanceof Collection){
						Collection vv = (Collection)v;
						if(vv.size()<1) continue;
					}
					
					//Object v = inner.get(k);
					newInner.put(k, v);
				}
				
				
				value = newInner.toString();
			}

			newMap.put(key, value);

		}
		
		return newMap;
	}
	
	@SuppressWarnings("unchecked")
	public static String toString(Object bean) throws Exception{
		String queryString = "";
		
		Map<String,Object> param = describe(bean);
		for (String key:param.keySet()){
			Object val = param.get(key);
			if (val!=null) {
				String value = val.toString();
				//String value = StringUtils.trimToEmpty(v[0]);
				queryString += key+"="+value+"&";
				
			}
		}
		
		return StringUtils.substringBeforeLast(queryString,"&");
	}

}
